home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / ALG1.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  157 lines

  1. /**************************************************************************
  2.  *
  3.  * alg1.cpp - Example program for STL generic algorithms that initialize
  4.  *    sequences.  Section 12.2
  5.  *
  6.  * $Id: alg1.cpp,v 1.3 1995/08/29 18:37:31 oberg Exp $
  7.  *
  8.  * $$RW_INSERT_HEADER "slyrs.cpp"
  9.  *
  10.  **************************************************************************/
  11.  
  12. # include <iostream.h>
  13. # include <vector>
  14. # include <list>
  15. # include <algorithm>
  16. # include <ctype.h>
  17. # include <string>
  18. # include <string.h>
  19.  
  20. using namespace std;
  21.  
  22. class iotaGen {
  23. public:
  24.     iotaGen (int iv) : current(iv) { }
  25.     operator () () { return current++; }
  26. private:
  27.     int current;
  28. };
  29.  
  30. void fill_example ()
  31.     // illustrate the use of the fill and fill_n functions
  32. {
  33.     cout << "Illustrate fill function" << endl;
  34.         // example 1, fill an array with initial values
  35.     char buffer[100], *bufferp = buffer;
  36.     fill(bufferp, bufferp + 100, '\0');
  37.     fill_n(bufferp, 10, 'x');
  38.     cout << buffer << endl;
  39.     
  40.         // example 2, use fill to initialize a list
  41.     list<string> aList;
  42.     fill_n (inserter(aList, aList.begin()), 10, "empty");
  43.     copy(aList.begin(), aList.end(), ostream_iterator<string>(cout, " ")), cout << endl;
  44.     
  45.         // example 3, use fill to overwrite values in a list
  46.     fill (aList.begin(), aList.end(), "full");
  47.     copy(aList.begin(), aList.end(), ostream_iterator<string>(cout, " ")), cout << endl;
  48.     
  49.         // example 4, fill in a portion of a list
  50.     vector<int> iVec(10);
  51.     generate (iVec.begin(), iVec.end(), iotaGen(1));
  52.     vector<int>::iterator seven = find(iVec.begin(), iVec.end(), 7);
  53.     fill(iVec.begin(), seven, 0);
  54.     copy(iVec.begin(), iVec.end(), ostream_iterator<int>(cout)), cout << endl;
  55. }
  56.  
  57. void copy_example()
  58.     // illustrate the use of the copy function
  59. {
  60.     cout << "Illustrate copy function " << endl;
  61.     char * source = "reprise";
  62.     char * surpass = "surpass";
  63.     char buffer[120], *bufferp = buffer;
  64.     
  65.         // example 1, a simple copy
  66.     copy(source, source + strlen(source) + 1, bufferp);
  67.     
  68.         // example 2, self copies
  69.     * copy(bufferp + 2, bufferp+ strlen(buffer), bufferp) = '\0';    
  70.     int buflen = strlen(buffer) + 1;
  71.     copy_backward(bufferp, bufferp + buflen, bufferp + buflen + 3);
  72.     copy(surpass, surpass + 3, bufferp);
  73.     
  74.         // example 3, copy to output
  75.     copy(bufferp, bufferp + strlen(buffer), ostream_iterator<char>(cout));
  76.     cout << endl;
  77.     
  78.         // example 4, use copy to convert type
  79.     list<char> char_list;
  80.     copy(bufferp, bufferp + strlen(buffer),inserter(char_list,char_list.end()));
  81.     char * big = "big ";
  82.     copy(big, big + 4, inserter(char_list, char_list.begin()));
  83.     
  84.     char buffer2[200], *buffer2p = buffer;
  85.     * copy(char_list.begin(), char_list.end(), buffer2p) = '\0';
  86.     cout << buffer2 << endl;
  87. }
  88.  
  89. #ifndef RWSTD_NO_LONG_HEADER_NAME
  90. # include <strstream.h>
  91. #else
  92. # include <strstrea.h>
  93. #endif
  94.  
  95. string generateLabel() {
  96.     // generate a label string of the form L_ddd
  97.     static int lastLabel = 0;
  98.     char labelBuffer[80];
  99.     ostrstream ost(labelBuffer, 80);
  100.     ost << "L_" << lastLabel++ << '\0';
  101.     return string(labelBuffer);
  102. }
  103.  
  104. void generate_example ()
  105.     // illustrate the use of the generate and genrate_n functions
  106. {
  107.     cout << "Illusustrate generate algorithm" << endl;
  108.     
  109.         // example 1, generate a list of label numbers
  110.     list<string> labelList;
  111.     generate_n (inserter(labelList, labelList.begin()), 4, generateLabel);    
  112.     copy (labelList.begin(), labelList.end(),
  113.             ostream_iterator<string>(cout, " ")), cout << endl;
  114.             
  115.         // example 2, generate an arithmetic progression
  116.     vector<int> iVec(10);
  117.     generate (iVec.begin(), iVec.end(), iotaGen(2));
  118.     generate_n (iVec.begin(), 5, iotaGen(7));
  119.     copy (iVec.begin(), iVec.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  120. }
  121.  
  122. void swap_example ()
  123.     // illustrate the use of the algorithm swap_ranges
  124. {
  125.     cout << "Illustrate swap_ranges algorithm" << endl;
  126.     
  127.         // first make two parallel sequences
  128.     int data[] = {12, 27, 14, 64}, *datap = data;
  129.     vector<int> aVec(4);
  130.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  131.     
  132.         // illustrate swap and swap_itr
  133.     swap(data[0], data[2]);
  134.     copy (data, data+4, ostream_iterator<int>(cout, " ")), cout << endl;
  135.     vector<int>::iterator last = aVec.end(); last--;
  136.     iter_swap(aVec.begin(), last);
  137.     copy (aVec.begin(), aVec.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  138.     
  139.         // now swap the entire sequence
  140.     swap_ranges (aVec.begin(), aVec.end(), datap);
  141.     copy (data, data+4, ostream_iterator<int>(cout, " ")), cout << endl;
  142.     copy (aVec.begin(), aVec.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  143. }
  144.  
  145. int main()
  146. {
  147.     cout << "STL generic algorithms -- initialization algorithms"
  148.         << endl;
  149.     fill_example();
  150.     copy_example();
  151.     generate_example();
  152.     swap_example();
  153.     
  154.     cout << "End of initialization tests" << endl;
  155.     return 0;
  156. }
  157.